/**
 * Copyright 2012
 * VAN
 */
package vaidyaa.algorithms.sorting;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author vaidyaa
 * 
 */
public class Main {

	public static void main(String[] args) throws IOException {
		Integer array[] = parseIntListFromFile("./data.txt");
		SortingCommons.printArray(array);
		System.out.println("*******************************");
		SortService sortService = new BubbleSortService();
		sortService.sort(array, false);
		SortingCommons.printArray(array);
		System.out.println("*******************************");
		sortService = new MergeSortService();
		sortService.sort(array, true);
		SortingCommons.printArray(array);
		System.out.println("*******************************");
		sortService = new QuickSortService();
		sortService.sort(array, false);
		SortingCommons.printArray(array);
		System.out.println("*******************************");
		sortService = new InsertionSortService();
		sortService.sort(array, true);
		SortingCommons.printArray(array);
		System.out.println("*******************************");
	}


	
	public static Integer[] parseIntListFromFile(String fileName) throws IOException {
		List<Integer> arrayList = new ArrayList<Integer>();
		FileInputStream fileInputStream = new FileInputStream(fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
		try {
		  while (true) {
		    String line = reader.readLine();
		    if (line == null) {
		    	break;
		    }
		    String[] fields = line.split(" ");
		    for (String field: fields) {
		    	arrayList.add(Integer.parseInt(field));
		    }
		  }
		} finally {
		  reader.close();
		}
		return arrayList.toArray(new Integer[0]);
	}

}
